package Stack;

import arrary.Array;

/**
 * @author zhaoziqiang
 * @Description:
 * @date 2020/8/14 21:08
 */
public class ArrStack<E> implements Stack<E> {
    Array<E> array;
    public ArrStack(int capacity){
        array=new Array<>(capacity);
    }
    public ArrStack(){
        array=new Array<>();
    }
    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public int getCapacity() {
        return array.getCapacity();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public void push(E e) {
       array.addLast(e);
    }

    @Override
    public E pop() {
        return array.removeLast();
    }

    /**
     * 查看栈顶元素
     * @return
     */
    @Override
    public E peek() {
        return array.getLast();
    }

    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder();
        sb.append("Stack: ");
        sb.append("[");
        for (int i = 0; i <array.getSize() ; i++) {
            sb.append(array.get(i));
            if (i!=array.getSize()-1)
                sb.append(",");
        }
        sb.append("]");
        sb.append(" top");
        return sb.toString();

    }
}
